package com.logic.modular.act.gm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.pojo.domain.logic.core.PvpMatch;
import com.comment.util.message.ResponseResult;
import com.logic.comment.util.RedisUtils;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.comment.pojo.domain.logic.domain.PlayerSkill;
import com.logic.modular.game.core.mapper.PvpMatchMapper;
import com.logic.modular.game.core.service.MatchEndServiceImpl;
import com.logic.modular.game.player.bag.service.BagItemServiceImpl;
import com.logic.modular.game.player.hero.service.PlayerHeroServiceImpl;
import com.logic.modular.game.player.skill.PlayerSkillServiceImpl;
import com.comment.pojo.domain.logic.core.CoreHero;
import com.comment.pojo.domain.logic.core.CoreSkill;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroServiceImpl;
import com.logic.modular.gameCore.cultivate.skill.service.CoreSkillServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.logic.modular.game.GameConstant.BANNED_TOKEN;


@Api(tags = "GM指令")
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/GMAct")
public class GMAction {
    private final CoreSkillServiceImpl coreSkillServiceImpl;
    private final PvpMatchMapper pvpMatchMapper;
    private final MatchEndServiceImpl matchEndServiceImpl;
    private final PlayerHeroServiceImpl playerHeroService;
    private final PlayerSkillServiceImpl playerSkillService;
    private final BagItemServiceImpl bagItemService;
    private final RedisUtils redisUtils;

    @ApiOperation(value = "指定武将获取兵力")
    @PostMapping("/obtainTroops")
    public ResponseResult<PlayerHero> obtainTroops(@RequestBody GMActionVo.obtainTroops obtainTroops) {
        return ResponseResult.success(
                "获取所有武将成功",
                playerHeroService.obtainTroops(
                        obtainTroops.getId()
                        , obtainTroops.getNum()
                )
        );
    }

    @ApiOperation(value = "批量武将获取兵力")
    @PostMapping("/obtainTroops2")
    public ResponseResult<List<PlayerHero>> obtainTroops(@RequestBody GMActionVo.obtainTroops2 obtainTroops) {
        return ResponseResult.success(
                "获取所有武将成功",
                obtainTroops.getId().stream().map(id -> playerHeroService.obtainTroops(id, obtainTroops.getNum())).collect(Collectors.toList())
        );
    }

    @ApiOperation(value = "获取所有武将可转化为信物")
    @PostMapping("/obtainAllHero")
    public ResponseResult<List<PlayerHero>> obtainAllHero(@RequestBody GMActionVo.obtainAllHero playerGet) {
        List<CoreHero> coreHeroes = CoreHeroServiceImpl.listCache();
        List<PlayerHero> accountHeroes = new ArrayList<>();
        for (CoreHero coreHero : coreHeroes) {
            accountHeroes.add(playerHeroService.obtainHero(playerGet.getUid(), coreHero.getId(), 1));
        }
        return ResponseResult.success("获取所有武将成功", accountHeroes);
    }

    @ApiOperation(value = "获取物品")
    @PostMapping("/obtainIOneWIngots")
    public ResponseResult<?> obtainIOneWIngots(@RequestBody GMActionVo.obtainIOneWIngots playerGet) {
        bagItemService.obtainBagItem(playerGet.getUid(), Long.valueOf(playerGet.getCorePropId()), new BigDecimal(playerGet.getNum()));
        return ResponseResult.success("成功获取获取");
    }

    @ApiOperation(value = "获取技能")
    @PostMapping("/obtainSkill")
    public ResponseResult<PlayerSkill> obtainSkill(@RequestBody GMActionVo.obtainSkill obtainSkill) {
        return ResponseResult.success("成功获取获取战法技能",
                playerSkillService.obtainSkill(
                        obtainSkill.getUid(),
                        obtainSkill.getSkillId()
                )
        );
    }

    @ApiOperation(value = "获取所有技能")
    @PostMapping("/obtainAllSkill")
    public ResponseResult<List<PlayerSkill>> obtainAllSkill(@RequestBody GMActionVo.obtainAllSkill obtainAllSkill) {
        List<CoreSkill> list = coreSkillServiceImpl.list().stream().filter(item -> item.getId() > 68).collect(Collectors.toList());
        return ResponseResult.success("成功获取获取战法技能",
                list.stream()
                        .map(playerSkill -> playerSkillService.obtainSkill(
                                        obtainAllSkill.getUid(),
                                        playerSkill.getId()
                                )
                        )
                        .collect(Collectors.toList())
        );
    }

    @ApiOperation(value = "结束所有对局(无结算),停止开游戏#1分钟")
    @GetMapping("/endAllMatch")
    public ResponseResult<?> endAllMatch(
            @RequestParam(value = "suspensionPeriod") Integer suspensionPeriod
    ) {
        redisUtils.set(BANNED_TOKEN, suspensionPeriod, suspensionPeriod, TimeUnit.MINUTES);
        List<PvpMatch> pvpMatches = pvpMatchMapper.selectList(new LambdaQueryWrapper<PvpMatch>().eq(PvpMatch::getStatus, 1));
        for (PvpMatch pvpMatch : pvpMatches) {
            matchEndServiceImpl.endGame(pvpMatch.getId());
        }
        return ResponseResult.success("结束所有对局,停止开游戏10分钟");
    }
}
